﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;



namespace Movement_Trajectory_Analysis
{
    public partial class MainForm : Form
    {
        //int sprawdzenie = 0;
        //int sprawdzeniecolor = 0;
        public static Color color;
        public MTA mta;
        public MLP mlp;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            mta = new MTA();
           
        }

        private void OpenVideoFileMenu_Click(object sender, EventArgs e)
        {
            /* BACKUP OLD AVI to BMP CONVERT METHOD
            OpenVideo.Filter = "Video file (*.avi)|*.avi";

            DialogResult ResultVideoOpen = OpenVideo.ShowDialog();
            if (ResultVideoOpen == DialogResult.OK)
            {
                MessageBox.Show("Choose output directory", "Info");
                
                DialogResult ResultVideoOutputBrowser = VideoOutputBrowser.ShowDialog();
                if (ResultVideoOutputBrowser == DialogResult.OK)
                {
                    try
                    {
                       
                        * // Create FFmpeg.exe proces. Convert video .avi to frames .jpeg
                        Process FFmpeg = new Process();

                        FFmpeg.StartInfo.FileName = @"ffmpeg.exe";
                        FFmpeg.StartInfo.Arguments = " -i " + @OpenVideo.FileName + " -r 24 -f image2 " + VideoOutputBrowser.SelectedPath + @"\%03d.jpeg";
                        FFmpeg.StartInfo.CreateNoWindow = true;

                        FFmpeg.Start();
                        FFmpeg.WaitForExit();
                        OpenPicture.InitialDirectory = VideoOutputBrowser.SelectedPath;
                        OpenPictureFileMenu_Click(sender, e);
                        //}
                        
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(exc.Message);
                    }
                }
            }
             * */

            AVI_Convert.AVIConvert aviConvert = new AVI_Convert.AVIConvert();
            aviConvert.ShowDialog();
        }

        private void OpenPictureFileMenu_Click(object sender, EventArgs e)
        {
            PictureList.Clear();
            ImageList ImageIcons = new ImageList();
            ImageIcons.ImageSize = new Size(64, 48);


            OpenPicture.Filter = "Picture file (*.jpeg; *.bmp; *.png)|*.jpeg; *.bmp; *.png";

            DialogResult ResultPictureOpen = OpenPicture.ShowDialog();
            if (ResultPictureOpen == DialogResult.OK)
            {
                pictureLoadingBar.Maximum = OpenPicture.FileNames.Length * 2;
                for (int i = 0; i < OpenPicture.FileNames.Length; i++)
                {
                    Image img = Image.FromFile(OpenPicture.FileNames[i]);
                    ImageIcons.Images.Add(img);
                    pictureLoadingBar.Value++;
                }

                PictureList.LargeImageList = ImageIcons;

                for (int i = 0; i < ImageIcons.Images.Count; i++)
                {
                    ListViewItem lstItem = new ListViewItem();

                    string FileName = OpenPicture.SafeFileNames[i];
                    FileName = FileName.Remove(FileName.Length - 5); //Remove extention from name

                    PictureList.Items.Add(FileName, i);
                    pictureLoadingBar.Value++;
                }
                pictureLoadingBar.Value = 0;
                PictureBox.Image = new Bitmap(OpenPicture.FileNames[0]);
            }
            
        }

        private void PictureList_ItemActivate(object sender, EventArgs e)
        {
            int index = PictureList.SelectedItems[0].Index;
            PictureBox.Image = new Bitmap(OpenPicture.FileNames[index]);
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            ColorList.Size = new Size(152, ActiveForm.Height - 150);
        }

        int colorIterator = 0; //Iterator of ColorList
        private void PictureBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            analyseToolsMenu.Enabled = true;
            Color color = new Color();
            ListViewItem listViewItem; 
            Bitmap bitmap = new Bitmap(PictureBox.Image);

            color = bitmap.GetPixel(e.X, e.Y);
            colorPanel.BackColor = color;

             /**********************************************************************************
             * Color selected by user in ListView object in order:
             * Item name is an iterator,
             * Component of the color: R,G,B,
             * Coordinates of the point: X,Y.
             * ********************************************************************************/

            listViewItem = new ListViewItem(colorIterator.ToString());
            listViewItem.SubItems.Add(color.R.ToString());
            listViewItem.SubItems.Add(color.G.ToString());
            listViewItem.SubItems.Add(color.B.ToString());
            listViewItem.SubItems.Add(e.X.ToString());
            listViewItem.SubItems.Add(e.Y.ToString());

            LabelXValue.Text = e.X.ToString() + " px";
            LabelYValue.Text = e.Y.ToString() + " px";

            ColorList.Items.Add(listViewItem);
            ColorList.Items[colorIterator].SubItems[0].BackColor = color;
            colorIterator++;
        }

        private void analyseToolsMenu_Click(object sender, EventArgs e)
        {
            if (ColorList.Items.Count != 0)
            {
                AnalyzeForm analyzeForm = new AnalyzeForm(this);
                analyzeForm.ShowDialog();
            }
            else
            {
                MessageBox.Show("Try to choose a color !","Warrning !");
            }
        }

        private void convertToolStripMenuItem_Click(object sender, EventArgs e)
        {

            convertForm zonvertForm = new convertForm(this);
            zonvertForm.ShowDialog();

        }

        private void PictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (OpenPicture.FileNames.Length != 0)
            {
                //Color priviewColor = new Color();
                Bitmap priviewBitmap = new Bitmap(PictureBox.Image);

                colorPanel.BackColor = priviewBitmap.GetPixel(e.X, e.Y);

                LabelXValue.Text = e.X.ToString();
                LabelYValue.Text = e.Y.ToString(); 
            }          
        }

        private void optionsToolMenu_Click(object sender, EventArgs e)
        {
            OptionsForm optionsForm = new OptionsForm(this);
            optionsForm.Show();
        }

        private void helpCreditsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HelpWindow helpwindow = new HelpWindow();
            helpwindow.Show();
        }
    }
}
